#include<string>
#include<vector>
#include<array>
#include<unordered_map>
#include<algorithm>
#include<limits>
#include <stack>
#include<iostream>
#include<sstream>
#include<deque>
#include<queue>
#define MOD 1000000007
class Solution {
public:
    std::vector<int> findOrder(int numCourses, std::vector<std::vector<int>>& prerequisites) {
        std::vector<int> du(numCourses,0);
        std::vector<std::vector<int>> biao(numCourses);
        int n = prerequisites.size();
        for (int i = 0; i < n; i++)
        {
            biao[prerequisites[i][1]].push_back(prerequisites[i][0]);
            du[prerequisites[i][0]]++;
        }
        std::queue<int> q;
        for (int i =0 ;i<numCourses;i++)
        {
            if (du[i] == 0)
            {
                q.push(i);
            }
        }
        int cnt = 0;
        std::vector<int> res;
        while(!q.empty())
        {
            int index = q.front();
            q.pop();
            res.push_back(index);
            cnt++;
    
            for (int b: biao[index])
            {
                du[b]--;
                if (du[b] == 0)
                {
                    q.push(b);
                }
            }
        }
        if (cnt != numCourses)
        {
            res.clear();
        }
      
           
           return res;
       
    }
};
